package Inji;

import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.ImageIcon;


@SuppressWarnings("serial")
public class Input extends JPanel { // JPanel을 상속했으므로, Input 패널,  is a 관계
	
	private JLabel lblNewLabel; // has a 관계
	private JLabel label;
	private JLabel label_1;
	private JLabel label_2;
	private JLabel label_3;
	private JTextField username;
	private JTextField address;
	private JTextField phone;
	private JTextField email;
	private JTextField age;
	private JButton confirm;
	
	ArrayList<Data6> list;
	private JLabel datalabel;
	private JLabel state;

	/**
	 * Create the panel.
	 */
	public Input() {
		setBackground(new Color(0, 102, 0));
		setLayout(null);
		add(getLblNewLabel());
		add(getLabel());
		add(getLabel_1());
		add(getLabel_2());
		add(getLabel_3());
		add(getUsername());
		add(getAddress());
		add(getPhone());
		add(getEmail());
		add(getAge());
		add(getConfirm());
		add(getDatalabel());
		add(getState());

	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("성명");
			lblNewLabel.setForeground(new Color(255, 255, 255));
			lblNewLabel.setFont(new Font("굴림", Font.PLAIN, 11));
			lblNewLabel.setBounds(300, 72, 57, 15);
		}
		return lblNewLabel;
	}
	private JLabel getLabel() {
		if (label == null) {
			label = new JLabel("주소");
			label.setForeground(new Color(255, 255, 255));
			label.setFont(new Font("굴림", Font.PLAIN, 11));
			label.setBounds(300, 109, 57, 15);
		}
		return label;
	}
	private JLabel getLabel_1() {
		if (label_1 == null) {
			label_1 = new JLabel("연락처");
			label_1.setForeground(new Color(255, 255, 255));
			label_1.setFont(new Font("굴림", Font.PLAIN, 11));
			label_1.setBounds(300, 146, 57, 15);
		}
		return label_1;
	}
	private JLabel getLabel_2() {
		if (label_2 == null) {
			label_2 = new JLabel("이메일");
			label_2.setForeground(new Color(255, 255, 255));
			label_2.setFont(new Font("굴림", Font.PLAIN, 11));
			label_2.setBounds(300, 183, 57, 15);
		}
		return label_2;
	}
	private JLabel getLabel_3() {
		if (label_3 == null) {
			label_3 = new JLabel("나이");
			label_3.setForeground(new Color(255, 255, 255));
			label_3.setFont(new Font("굴림", Font.PLAIN, 11));
			label_3.setBounds(300, 220, 57, 15);
		}
		return label_3;
	}
	private JTextField getUsername() {
		if (username == null) {
			username = new JTextField();
			username.setBounds(360, 67, 116, 21);
			username.setColumns(10);
		}
		
		
		
		return username;
	}
	private JTextField getAddress() {
		if (address == null) {
			address = new JTextField();
			address.setColumns(10);
			address.setBounds(360, 105, 116, 21);
		}
		return address;
	}
	private JTextField getPhone() {
		if (phone == null) {
			phone = new JTextField();
			phone.setColumns(10);
			phone.setBounds(360, 143, 116, 21);
		}
		return phone;
	}
	private JTextField getEmail() {
		if (email == null) {
			email = new JTextField();
			email.setColumns(10);
			email.setBounds(360, 181, 116, 21);
		}
		return email;
	}
	private JTextField getAge() {
		if (age == null) {
			age = new JTextField();
			age.setColumns(10);
			age.setBounds(360, 219, 116, 21);
		}
		return age;
	}
	private JButton getConfirm() {
		if (confirm == null) {
			confirm = new JButton("확인");
			confirm.setBackground(new Color(255, 255, 255));
			confirm.setFont(new Font("굴림", Font.PLAIN, 11));
			confirm.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					
					boolean flag2 = true;
					
					// 데이터 입력 여부 검사 
					boolean flag = true; // 모든 값을 입력했을때 
					if(username.getText().equals("")) // 공백이라면 
						flag = false; 
					else if (address.getText().equals(""))
						flag = false;
					else if (phone.getText().equals(""))
						flag = false;
					else if (email.getText().equals(""))
						flag = false;
					else if (age.getText().equals(""))
						flag = false;
					
					if (flag){
						
						char number = age.getText().charAt(0); 
						
						if (!(number >= 48 && number <= 57))
						{
							datalabel.setText("나이는 숫자를 입력해야 합니다");
							age.setText("");
							
						}
						else{
							
							Date date = new Date();
							
							Data6 d = new Data6(username.getText(), address.getText(), 
									phone.getText(), email.getText(), Integer.parseInt(age.getText()), date); 
						
							// ArrayList에 추가
							list.add(d);
							state.setText("데이터가 저장되었습니다.");
							
							username.setText("");
							address.setText("");
							phone.setText("");
							email.setText("");
							age.setText("");
							
						}			
					}
					
				}
			});
			confirm.setBounds(379, 260, 97, 23);
		}
		return confirm;
	}
	
	public void setD (ArrayList<Data6> list){
		this.list = list;  
	}
	private JLabel getDatalabel() {
		if (datalabel == null) {
			datalabel = new JLabel("            Insert Date");
			datalabel.setOpaque(true);
			datalabel.setForeground(Color.WHITE);
			datalabel.setFont(new Font("Consolas", Font.PLAIN, 17));
			datalabel.setBackground(Color.DARK_GRAY);
			datalabel.setBounds(256, 20, 220, 21);
		}
		return datalabel;
	}
	private JLabel getState() {
		if (state == null) {
			state = new JLabel("데이터를 입력하세요.");
			state.setForeground(new Color(255, 255, 204));
			state.setFont(new Font("굴림", Font.PLAIN, 11));
			state.setBounds(12, 309, 437, 15);
		}
		return state;
	}
}
